package com.spzx.product.controller;

import com.alibaba.excel.EasyExcel;
import com.spzx.common.core.domain.R;
import com.spzx.common.core.web.controller.BaseController;
import com.spzx.common.core.web.domain.AjaxResult;
import com.spzx.common.security.annotation.InnerAuth;
import com.spzx.product.api.domain.vo.CategoryVo;
import com.spzx.product.domain.Category;
import com.spzx.product.service.CategoryService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;

/**
 * @ClassName CategoryController
 * @Author 波
 * @Date 2024/7/29 18:42
 */
@RestController
@RequestMapping("/category")
@Tag(name = "商品分类接口")
public class CategoryController extends BaseController {
    @Autowired
    private CategoryService categoryService;

    @GetMapping("/{id}")
    @Operation(summary = "根据id获取商品分类信息")
    public AjaxResult getCategoryById(@PathVariable("id") Long id) {
        List<Category> categoryList = categoryService.getCategoryById(id);
        return AjaxResult.success(categoryList);
    }

    @PostMapping("download")
    @Operation(summary = "文件导出")
    public void download(HttpServletResponse response) throws IOException {
        categoryService.download(response);
    }
    @PostMapping("upload")
    @Operation(summary = "文件导入")
    public AjaxResult upload(MultipartFile file) throws IOException {
        categoryService.upload(file);
        return success();
    }

    /**
     * 查询所有一级分类列表
     * @return
     */
    @InnerAuth
    @GetMapping("/getOneCategory")
    public R<List<CategoryVo>> getOneCategory() {
        return R.ok(categoryService.getOneCategory());
    }

    /**
     * 查询所有分类信息
     * @return List<CategoryVo>
     */
    @InnerAuth
    @GetMapping("/tree")
    public R<List<CategoryVo>> getCategoryTree() {
        List<CategoryVo> categoryVoList=categoryService.getCategoryTree();
        return R.ok(categoryVoList);
    }
}
